package zero.tech.games.gate.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import zero.tech.core.data.JwtUtil;
import zero.tech.core.data.Resp;
import zero.tech.core.data.SimpleUser;
import zero.tech.games.api.RemoteUser;
import zero.tech.games.common.Code;
import zero.tech.games.common.RedPointType;
import zero.tech.games.entity.games.UserRole;
import zero.tech.games.entity.user.TUser;
import zero.tech.games.entity.user.TUserAccount;
import zero.tech.games.gate.data.GATECMD;
import zero.tech.games.gate.data.Redpoint;
import zero.tech.games.gate.data.entity.ClubPipei;
import zero.tech.games.gate.data.notify.NotifyRedpoint;
import zero.tech.games.gate.data.res.GateResLogin;
import zero.tech.games.gate.hot.MailUtil;
import zero.tech.games.gate.hot.UserUtil;
import zero.tech.games.gate.service.api.IGateService;
import zero.tech.games.gate.util.PackageUtil;
import zero.tech.games.gate.util.PipeiUtil;
import zero.tech.wsnet.controller.CacheOnline;
import zero.tech.wsnet.controller.GateController;
import zero.tech.wsnet.net.WebClient;

@Service
public class GateServiceImpl implements IGateService{
	@Autowired
	private RemoteUser remoteUser;
	@Autowired
	private UserUtil userUtil;
	@Autowired
	private PipeiUtil pipeiUtil;
	@Autowired
	private MailUtil mailUtil;
	@Override
	public void gateReqLogin(WebClient webClient, String token) {
		SimpleUser	simpleUser = JwtUtil.parseJWT(token);
		if (simpleUser == null) {
			webClient.sendMessage(GATECMD.GATE_RES_LOGIN,Resp.build(Code.TOKEN_UNAVAILABLE, "token错误或者已失效,请重新登陆"));
			return;
		}
		int uid=simpleUser.getUid();
		TUser user = remoteUser.getUser(uid);
		if (user == null) {
			webClient.sendMessage(GATECMD.GATE_RES_LOGIN,Resp.build(Code.USER_NOT_EXIST, "用户不存在"));
			return;
		}
		GateController controller = new GateController(webClient, simpleUser);
		CacheOnline.getCache().online(controller);
		TUserAccount account = remoteUser.getUserAccount(uid);
		//检查用户是否在匹配队列中
		ClubPipei clubPipei=pipeiUtil.getClubPipei(simpleUser.getUid());
		GateResLogin gateResLogin=PackageUtil.packageGateResLogin(simpleUser,account,clubPipei);
		webClient.sendMessage(GATECMD.GATE_RES_LOGIN, Resp.build(Code.OK, "登陆成功",gateResLogin));
		//开始推送红点信息
		pushRedpoint(controller);
	}
	private void pushRedpoint(GateController controller) {
		//1.检查邮件红点
		int uid=controller.getId();
		int not_read_mail_count=mailUtil.findNotReadMailCount(uid);
		NotifyRedpoint notifyRedpoint = new NotifyRedpoint();
		if(not_read_mail_count>0) {
			notifyRedpoint.getRedpoints().add(new Redpoint(RedPointType.NOT_READ_MAIL,not_read_mail_count));
		}
		if(!notifyRedpoint.getRedpoints().isEmpty()) {
			controller.getWebClient().sendMessage(GATECMD.NOTIFY_REDPOINT,Resp.build(Code.OK, "红点信息到达",notifyRedpoint));
		}
	}
	@Override
	@Transactional
	public void initGateData(int uid) {
		initUserRole(uid);
	}
	private void initUserRole(int uid) {
		UserRole userRole = userUtil.getUserRole(uid);
		if(userRole==null) {
			userRole=new UserRole(uid, 0);
			userUtil.insertUserRole(userRole);
		}
	}
}
